package com.atguigu.gulimail.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimail.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000119637523";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCK9I05orJuB/u65lsnTcys6PKpCWC/ZQXe9jW1WDMOC79kBiWQIkUAmelKi73uen7lrEvZsZ0Ivv+1id2p5wfQ4FwgpmCiw46wUnJ8EgFj4QG2ghwL9O0w7GbUBB6HgoVEwoek/tfkSmZ2bzP0XBXxSInfSkrqQm1HMJ2SrL3TXwlMm8QbsL1o2KZMweGRA05kYavKuLD8EfVhEwkSp3dcsd1IiIw2GiekergT64D8pHUOJYNbLuIU3wcYjDJ7eRm7ukPoFrHvWFsvFE+fFyynffPQ7mNT5/MXM/bpQUKlhhQ6En76wC7G1jk/0cNCJjw82FugPrcG1WUjfRXMzNQFAgMBAAECggEAZwPKNzJg6aXNf9ALP+YzmPTSAXNMYFg6tAL/6kNLBly7NSJBcsuVME7qXzOeSVBTB15mlStR6gv/csyUosL+TmImqqXt+nzUKe/VpBmNIkjLhkyLEmov9hcsXH35TCj9N+6ntTChSUPHFoJ2l/A07D4EyJ9BTs6USTTHfNfHmw0+yCuofUgr1y5Ogp7nC2ilmOn160SLvjiE0Rd0wfBLwEb2O2tq6pLn7jsHqrg6scPonsNKQ6BqoLUbe2211p4xkwShtgl4sS8gHAJcO00zIrJUYn1BK92LlK0Yv/ULELBrh21aDiILJ5bqAlxlY1lRxk/+5eD4eKPPVLIZUtZZeQKBgQDQNusN5ivKGV7mEHJwwoe1wswmpfFN7QOdkEwVr6IqOUjyqHxAA4hBqNkTzQuePEQlTrNT3UwOYKw+2YAwRGBf8P2+PnChMSe08o5xwFKs5Yp4Q3k6U+Ie5Vx2rQ/FPLww6TCsAhPzgMI+p2MJKuHd/wN6MKD67ddMy8oHjRtyfwKBgQCq2H5jpjhNMU3yLk7ydvSm0VLXeIA9Tr8UxlUhY2/KIyHQV2Por+ZDWACp3QKckX1J/ZQiMs9EXtTQUMlhcg5+CPZZ1OFDnfSIoRBK83I3RxdZLFoMMZxjb8/yvFN9nGgSrLH5ROzi0c/PgmKGdLmY/i7j1CurYDevfJXi5qivewKBgQCcGsCKECp+gBz9IhGa5s32gRCsXTeYM/3E8P2KOL69J2vAun9FQyCyEZSG2vE99nMlAWdDliPCl8or6MfJ6EnVRIoJSykIQSFC8aPINugFRqdVWUkXaPKcNVvoggHT36A8j2h1Xa11n/CCJt8CbYsf9GS4BKN83jrQEe3rUWjlowKBgH0LDZ6jxyuAfbtW94NxW2lJZee9E6dLnoF3FFHmtKhvBE232LEzJUZ+/6VT1JiByADCr4xEQf2Zpwt9RZej4kWPC7f+Ho3d61+g+wrQmfUpfEZRNusb2InTCZFxE26pbmbUrUJzlyoARKhtlu0wO7Nlvy4TWY8KUleHyTCUgDxHAoGBAIB4iJGXwvtaTRzyceSnId6JxAc4msphD5DGwz6xy221vzGQXKl2sMWtaMsysFXBdqnWglF9k3CcqBPmHZVGzS+QQoW9AxqgwTFCanwPttdktWPvT7QGcwxPvYVo+cnnSbCuSv19mc76kfFqFnYGv4slGCjRgGLYgJPVdZwl1yBX";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAr+1GdYE3A2jm4Hn6aR/8Se+HvlxxQ7RLhLKxn2sEfLb3XTgQIiXhrTNbYqGecIbT5zPCqNwRA/4G4ZqvAgDtoAiKumk51msVYtXjuwvjxJiKKOGd4Ust/Ji2wXx+pjYytNpkIZqJDkmhGZ81eGn8PHNpZrQRN+CoSkwySCFFgk61hUeuLZQQlK9cLH5JZE/aq6goSnC+hF0/agLoIPLDE51Ymoq3jkjCR7W+IUnd+mhcjPwbP9nLbuVCCHRJEZRZDxTW/Xugw+oftjfQJsQ3V+F5NaTVxf9F27sKRGzQxyWGTogWzfwWCSvl0GdVFxULS7258DaNhke4OLyZpTSX+wIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://dingabcde.vaiwan.cn/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimail.com/memberOrder.html";

    // 签名方式
    private String sign_type = "RSA2";

    // 字符编码格式
    private String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";


    private String timeout = "1m";

    public String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\"" + out_trade_no + "\","
                + "\"total_amount\":\"" + total_amount + "\","
                + "\"subject\":\"" + subject + "\","
                + "\"body\":\"" + body + "\","
                + "\"timeout_express\":\""+timeout+"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应：" + result);

        return result;

    }
}
